<template>
  <el-card class="form-container" shadow="never">
    <el-form :model="workshop" :rules="rules" ref="wsFrom">
      <el-row :gutter="20">
        <el-col :span="18">
          <el-form-item label="选择当前车间：" prop="id" required>
            <el-select v-model="workshop.id"
                      placeholder="请选择">
              <el-option
                v-for="item in selectList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <el-button type="primary" @click="onSubmit('wsFrom')">提交</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-card>
</template>

<script>
  import { workshopsByRole } from '@/api/workshop';

export default {
  name: 'setWorkshop',
  data() {
    return {
      workshop: {
        id: null
      },
      selectList: [],
      rules: {
        id: [
          {required: true, message: '请选择车间', trigger: 'blur'}
        ],
      }
    }
  },
  created(){
    this.getWorkshopId();
    this.workshopSelects();
  },
  methods: {
    workshopSelects() {
      workshopsByRole().then(response=>{
        this.selectList = response.data;
      })
    },
    getWorkshopId() {
      let id =  window.localStorage.getItem('kanban_workshopId')
      if(id) {
        this.workshop.id = parseInt(id)
      }
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$confirm('是否提交数据', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
                window.localStorage.setItem('kanban_workshopId', this.workshop.id)
                this.$message({
                  message: '修改成功',
                  type: 'success',
                  duration: 1000
                });
            })
        }
      })
    }
  }
}
</script>
